package cn.com.pxto.dao;

import java.util.List;

import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.dao.pagesupport.hibernate.HibernatePageDaoSupport;
import cn.com.pxto.model.Lesson;

public class LessonDAO extends HibernatePageDaoSupport {
	public Lesson getLesson(Integer lid){
		return (Lesson) getTemplate().get(Lesson.class, lid);
	}
	
	public void saveLesson(Lesson lesson){
		getTemplate().save(lesson);
	}
	
	public void deleteLesson(Integer lid){
		String HQL="from Lesson as l where l.lid="+lid;
		getTemplate().delete(HQL);
	}
	
	public void updateLesson(Lesson lesson){
		getTemplate().saveOrUpdate(lesson);
	}
	/*
	 * return null if cant find
	 */
	@SuppressWarnings("unchecked")
	public Lesson getLesson(String name){
		String HQL="select l from Lesson as l where l.name='"+name+"'";
		List list=getTemplate().find(HQL);
		if(list!=null && list.size()>0){
			return (Lesson)list.get(0);
		}else{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Lesson> listLesson(Integer type,PageInfo pageInfo){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select l from Lesson as l  ");
		HQL.append(" where l.type="+type);
		HQL.append(" order by l.lid");
		return getTemplate().pagedFind(HQL.toString(), pageInfo);
	}
	
	@SuppressWarnings("unchecked")
	public List<Lesson> listLesson(PageInfo pageInfo){
		String HQL="select l from Lesson as l order by l.lid";
		return getTemplate().pagedFind(HQL, pageInfo);
	}
	
	@SuppressWarnings("unchecked")
	public List<Lesson> listLesson(){
		String HQL="select l from Lesson as l order by l.lid";
		return getTemplate().find(HQL);
	}
	
	/*
	 * 根据名称模糊查询
	 */
	public List<Integer> getLessonByName(String name){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select l.lid from Lesson as l where l.name like '%");
		HQL.append(name+"%'");
		HQL.append(" order by l.lid");
		return getTemplate().find(HQL.toString());
	}
}
